package com.wanhai.itm.util;

import com.wanhai.util.UserInfoUtil;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.GenericInitiator;

public class AuthenticInitiator extends GenericInitiator {

    private static final String DEFAULT_LOGIN_PATH = "/login.zul";
    //services
    //AuthenticationService authService = new AuthenticationServiceChapter8Impl();

    public void doInit(Page page, Map<String, Object> args) throws Exception {
        HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
        if (UserInfoUtil.getUserBean(request) == null) {
            String user_code = "M1933";
            String office_code = "TWTPE01";
            String prog_code = "WDCMS124";
            String prog_name = "Template1 - Single Master Table";
            String time_diff = "0";

            UserInfoUtil.assignUserInfo(
                    request,
                    user_code,
                    office_code,
                    prog_code,
                    prog_name,
                    time_diff);
        }

//        System.out.println("ID: " + page.getId());
//        System.out.println("Request Path: " + page.getRequestPath());
//        System.out.println("Title: " + page.getTitle());
//
//        if (!page.getRequestPath().endsWith(DEFAULT_LOGIN_PATH)) {
//            if (!isLogin(page)) {
//                Execution exec = Executions.getCurrent();
//                exec.sendRedirect(DEFAULT_LOGIN_PATH);
//            }
//        }
    }

    private boolean isLogin(Page page) {
        return true;
    }
}